<extend name="Public/content" />
<block name="style">
<style type="text/css" >
    .red{color: red;}
    body{ background: #fff;}
    .footer {
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 0;
   
    left: 0;
    line-height: 50px;
    text-align: right;
    text-align: right;
    border-top: solid 1px #eee;
    padding-right: 10px;
    z-index: 999999999999999;
}

.checkbox {
    display: inline-block;
     width: 35%;
}
.row_gc{ width: 100% !important;}
.col-xs-12ga_c{ padding: 20px 0 20px 30px;}
</style>
</block>
<block name="body">
    <!-- 弹窗插件 -->
    <include file="Public:artdialog" />

 <div class="row"  >
        <div class="col-md-12" >
            <div class="block-web">
                <div class="header">
                    <h3 class="content-header"><if condition="$is_add eq 1">新增<else/>编辑</if>管理员</h3>
                </div>
                <div class="porlets-content">
                    <form class="form-horizontal row-border vform_show" id="searchForm" action="#">
                        <table style="table-layout: fixed;width: 100%;">
                            <tr>
                                <td width="380px" style="vertical-align:top;">
                                    <input type="hidden" name="is_add" value="{$is_add}">
                                    <input type="hidden" name="id" value="{$m['id']}">
                                    <input type="hidden" name="rules" id="rules" value="{$m['power']}">
                                       <div class="form-group">
                                        <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 管理员名称 </label>
                                        <div class="col-sm-9">
                                            <input type="text" id="username" name="username" placeholder="请输入管理员名称" value="{$m['username']}"  onblur="check_old_pwd(this,'username')" class="col-xs-10">
                                                <span  style="display:none"> <i class="icon-ok green" ></i></span>
                                        </div>
                                    </div>
                                   <if condition="$is_add eq 1">
   <!--                                  <div class="form-group">
                                        <label class="col-sm-3 control-label">密码</label>
                                        <div class="col-sm-8">
                                            <input type="password" name="pwd" class="form-control"  value="" datatype="*"  nullmsg="请输入密码" >
                                            <span  style="display:none"> <i class="icon-ok green" ></i></span>
                                        </div>
                                      
                                    </div> -->
                                       <div class="form-group">
                                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 原密码 </label>

                                        <div class="col-sm-9">
                                            <input type="password" id="pwd" name="pwd"  placeholder="请输入密码" onblur="check_old_pwd(this,'pwd')" class="col-xs-10">
                                              <span  style="display:none"> <i class="icon-ok green" ></i></span>
                                               
                                        </div>
                                    </div>
                                </if>

                                       <div class="form-group">
                                        <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 真实姓名 </label>

                                        <div class="col-sm-9">
                                            <input type="text" id="realname" name="realname" placeholder="请输入真实姓名" value="{$m['realname']}"  onblur="check_old_pwd(this,'realname')" class="col-xs-10">
                                                <span  style="display:none"> <i class="icon-ok green" ></i></span>
                                        </div>
                                    </div>

                                       <div class="form-group">
                                        <label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 手机号 </label>

                                        <div class="col-sm-9">
                                            <input type="text" id="mobile" name="mobile" placeholder="请输入联系方式" value="{$m['mobile']}"  onblur="check_old_pwd(this,'mobile')" class="col-xs-10">
                                                <span  style="display:none"> <i class="icon-ok green" ></i></span>
                                        </div>
                                    </div>
<!-- 
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">排序</label>
                                        <div class="col-sm-8">
                                            <input type="text" name="sort" class="form-control" value="{$m['sort']|default='0'}" datatype="n" nullmsg="请输入排序">
                                            <div class="Validform_checktip"></div>
                                        </div>

                                        <span class="red">*</span>

                                    </div><!--/form-group--> 


                                    <if condition="$is_add eq 1">
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">是否有效</label>
                                            <div class="col-sm-9">
                                                <label class="radio-inline">
                                                    <input type="radio"  <if condition="$status eq 1">checked</if> name="status" value="1" id="inlineradio1" >
                                                    <span class="custom-radio"></span> 启用 </label>
                                                <label class="radio-inline">
                                                    <input type="radio"  name="status"  <if condition="$status eq 0">checked</if>  value="0" id="inlineradio2">
                                                    <span class="custom-radio"></span> 禁止 </label>
                                            </div>
                                        </div>
                                        <else/>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">是否有效</label>
                                            <div class="col-sm-9">
                                                <label class="radio-inline">
                                                    <input type="radio"  checked name="status" value="1" id="inlineradio1" >
                                                    <span class="custom-radio"></span> 启用 </label>
                                                <label class="radio-inline">
                                                    <input type="radio"  name="status" value="0" id="inlineradio2">
                                                    <span class="custom-radio"></span> 禁止 </label>
                                            </div>
                                        </div>
                                    </if>


                                </td>
                                <td style="padding-left: 50px;">
<!--                                     <div class="form-group">
                                        <label class="col-sm-3 control-label">权限：</label>
                                        <div class="col-sm-9">
                                            <div style="width: 100%;">
                                                <a href="javascript:;" onclick="checkAllTrue()" class="btn btn-success btn-xs">全选</a>&nbsp;
                                                <a href="javascript:;" onclick="checkAllFalse()" class="btn btn-success btn-xs">取消</a>&nbsp;
                                                <a href="javascript:;" onclick="expandAll()" class="btn btn-success btn-xs">展开</a>&nbsp;
                                                <a href="javascript:;" onclick="collapseAll()" class="btn btn-success btn-xs">折叠</a>&nbsp;
                                            </div>
                                            <div style="width: 100%;">
                                                <ul id="treeRule" class="ztree"></ul>
                                            </div>
                                        </div>
                                    </div> -->

                                   <div class="col-xs-12 ">
                                        <input name=""  class="ace ace-checkbox-2" value=""  id="alldel" type="checkbox">
                                           <span class="lbl lbl_gc">全选</span> 
                                           <hr>
                                   <volist name="group_list" id="vo">
                                         
                                       <span class=" ">
                                            <div class="checkbox">
                                                <label>
                                                <if condition="$vo.status eq 1">
                                                 <input name="group_id_{$vo.id}" checked="checked"  class="ace ace-checkbox-2"  value="{$vo.id}" type="checkbox">
                                                <else/>
                                                    <input name="group_id_{$vo.id}"  class="ace ace-checkbox-2"  value="{$vo.id}" type="checkbox">
                                                 </if>   
                                                    <span class="lbl lbl_gc">{$vo.name}</span>
                                                </label>
                                            </div>
                                        </span>
                                             
                                   </volist>
                              </div>
                                </td>
                            </tr>
                        </table>
                        <input type="hidden" name="group_ids" id="group_ids" value="" >
                        <div class="footer" >
                            <button class="btn btn-primary btn-success bt_submit" type="button" onclick="sub_quanxian()">提交</button>
                            <button class="btn btn-default bt_close" type="button" onclick="art.dialog.open.api.close();" >关闭</button>
                        </div>
                    </form>
                </div><!--/porlets-content-->
            </div><!--/block-web-->
        </div><!--/col-md-6-->

    </div>

</block>
<block name="script">
    <!-- 树 -->
    <include file="Public:tree" />
    <script type="text/javascript">
      _hide_public_loading();
     var is_add = {$is_add};
     var is_username = 0;
     var is_pwd = 0;
     var is_realname = 0;
     var is_mobile = 0;
   $(function(){
    $("#alldel").bind("click",checkSel);
   })

      function checkSel(){
       var flg= $(this).is(":checked");
       $("input[type='checkbox']").prop("checked",flg);
    }


   function selallval(){
     var _list="";
     $("input[type='checkbox']:checked").each(function(){
   
         // var flg=$(this).is(":checked");
         //if(!flg)return false;
         var _val=$(this).val();
         if(_val!=""){
         _list+= (_list==""?" ":",")+_val;
         } 
     });
     return _list;
} 
/*     var setting = {
            view: {
                selectedMulti: false
            },
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        var treeObj;
        function createTree() {
            $.post("{:U('Nav/menu_ajax',array('id'=>$id))}",function(resultobj){
                treeObj= $.fn.zTree.init($("#treeRule"), setting, resultobj);
                 treeObj.expandAll(true);
            });



        }

        $(function(){
            createTree();
        });
        function checkAllTrue(){
            treeObj.checkAllNodes(true);
        }
        function checkAllFalse(){
            treeObj.checkAllNodes(false);
        }
        function expandAll(){
            treeObj.expandAll(true);
        }
        function collapseAll(){
            treeObj.expandAll(false);
        }*/

  function check_old_pwd(_this,type){
      //验证密码
      if(type =="username"){
        var username = $(_this).val();
        if(username == "" || username.length<2 || username.length >16){
             $(_this).parent().children('span').show();
             $(_this).parent().children('span').html('<i class="icon-remove red"></i>');
             return false; 
         }else{
                  var url = "{:U('User/is_username')}";
                  $.ajax({
                            'url':url,
                            'data':{username:username},
                            'dataType':'json',
                            'type':'post',
                            success:function(data){
                               
                                var retstatus = data.state == 1 ? "1" : "0";
                                  $(_this).parent().children('span').show();
                                if(retstatus == 1){
                                   is_username =1 ;
                                   $(_this).parent().children('span').show();
                                    $(_this).parent().children('span').html('<i class="icon-ok green"></i>');
                                }else{
                                    $(_this).parent().children('span').show();
                                    $(_this).parent().children('span').html('<i class="icon-remove red"></i>');
                                     is_username = 0;
                                    // showvfmsg(retstatus, data.msg, 1900);
                                }
                            },error:function(){
                                showvfmsg('error','请求服务器失败',2000);
                            },complete:function(){

                            }
                        });  
/*           $(_this).parent().children('span').show();
           $(_this).parent().children('span').html('<i class="icon-ok green"></i>');
           is_username =1 */
         }
      }

      if(type=="realname"){
          var realname = $(_this).val();
            if(realname == ""){
                 $(_this).parent().children('span').show();
                 $(_this).parent().children('span').html('<i class="icon-remove red"></i>');
                 return false; 
             }else{
               $(_this).parent().children('span').show();
               $(_this).parent().children('span').html('<i class="icon-ok green"></i>'); 
               is_realname =1;
             }
      }

      if(type=="mobile"){
          var mobile = $(_this).val();
           var res = isMobileNo(mobile);
            if(!res){
                 $(_this).parent().children('span').show();
                 $(_this).parent().children('span').html('<i class="icon-remove red"></i>');
                 return false; 
             }else{
               $(_this).parent().children('span').show();
               $(_this).parent().children('span').html('<i class="icon-ok green"></i>'); 
               is_mobile =1;
             }
      }
     
    
      if(is_add == 1){
         if(type=="pwd"){
             var pwd = $(_this).val();
                  if(pwd == "" || pwd.length<4 || pwd.length >16){
                     // showvfmsg(0, '密码格式错误！', 1900);
                     $(_this).parent().children('span').show();
                     $(_this).parent().children('span').html('<i class="icon-remove red"></i>');
                     return false;
                  }else{
                      $(_this).parent().children('span').show();
                     $(_this).parent().children('span').html('<i class="icon-ok green"></i>');
                     is_pwd =1;
                  }
         }
      }
  }

  function isMobileNo(mobile) { 
     var pattern =  /^\+?[1-9][0-9]*$/; 
     return pattern.test(mobile); 
   } 
function  sub_quanxian(){
             // huoquanxian();
            //获取权限组id
    
           var ids= selallval();
            if(ids == ""){
                showvfmsg(0, '请选择权限', 2000); return;
            }
            $('#group_ids').val(ids);
             if(is_add ==1){
                 if(is_pwd != 1){
                    showvfmsg(0, '信息填写有误', 2000); return;
                 }
                   if(is_username!=1 ||is_realname!=1 || is_mobile!=1 ){
                      showvfmsg(0, '信息填写有误', 2000); return;
                   }
             }else{
                   if($('#username').val() =="" ||$('#realname').val() =="" || $('#mobile').val() =="" ){
                      showvfmsg(0, '没有修改', 2000); return;
                   }
             }

            // console.log($('#searchForm').serialize());
            showvfmsg("-1", '提交中，请稍后...', 999999); 
             var url="{:U('Nav/edit_admin')}";
                $.ajax({
                    'url':url,
                    'data':$('#searchForm').serialize(),
                    'dataType':'json',
                    'type':'post',
                    beforeSend:function(){
                         $("#submit").attr("disabled",'disabled');
                         $("#submit").html("提交中...");
                         $(".bt_submit").attr("disabled", 'disabled');
                         $(".bt_submit").html("提交中...");
                    },
                    success:function(data){
                            $("#submit").removeAttr('disabled');
                            $("#submit").html("提交");
                            $(".bt_submit").removeAttr('disabled');
                            $(".bt_submit").html("提交");
                            var retstatus = data.state == 1 ? "1" : "0";
                            showvfmsg(retstatus, data.msg, 1900);
                            if(retstatus ==1){
                               setTimeout(function(){
                                window.parent.load();
                                art.dialog.open.api.close();

                                // self.opener.location.reload(); 
                            },1000)

                            }
                        // goSearch("#searchForm","order_list_item_content");

                        //_reload_currentpage_datalist('order_list_item_content');
                        // goSearch("#searchForm","order_list_item_content");


                    },error:function(){
                        showvfmsg('error','请求服务器失败',2000);
                    },complete:function(){
                            $("#submit").removeAttr('disabled');
                            $("#submit").html("提交");
                            $(".bt_submit").removeAttr( 'disabled');
                            $(".bt_submit").html("提交");
                    }
      })
       
}

    function load(){
        goSearch("#searchForm","order_list_item_content");
    }

function huoquanxian(){
        var nodes=treeObj.getCheckedNodes(true);
        var newArrays=new Array();
        for(var  i=0;i<nodes.length;i++){
            newArrays.push(nodes[i].id);
        }
        // console.info(newArrays);
        $("#rules").val(newArrays.join(","));
    }


    </script>
</block>